package com.rick.awarenessdemo

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import com.amap.api.services.core.PoiItem
import com.amap.api.services.poisearch.PoiResult
import com.amap.api.services.poisearch.PoiSearch
import com.blankj.utilcode.util.ToastUtils
import com.rick.awarenessdemo.base.BaseActivity
import com.rick.awarenessdemo.base.BaseAdapter
import com.rick.awarenessdemo.databinding.ActivitySetBinding
import com.rick.awarenessdemo.databinding.ItemPoiBinding
import com.rick.awarenessdemo.util.AwarenessUtil
import com.rick.awarenessdemo.util.MapUtil

/**
 * 设置目的地
 */
class SetActivity :
    BaseActivity<ActivitySetBinding>(R.layout.activity_set), TextWatcher {

    private val adapter = object : BaseAdapter<PoiItem, ItemPoiBinding>(R.layout.item_poi) {
        override fun init(
            binding: ItemPoiBinding,
            bean: PoiItem,
            position: Int
        ) {
            binding.poiItem = bean
            binding.root.setOnClickListener {
                MapActivity.startForResult(
                    this@SetActivity,
                    bean,
                    0
                )
            }
        }
    }

    override fun init(savedInstanceState: Bundle?, binding: ActivitySetBinding) {
        binding.input.addTextChangedListener(this)
        binding.input.requestFocus()
        binding.list.adapter = adapter
        binding.list.layoutManager = LinearLayoutManager(this)
        binding.list.addItemDecoration(DividerItemDecoration(this, DividerItemDecoration.VERTICAL))
    }

    override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
    }

    /**
     * 当输入变化
     */
    override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
        val keyword = s.toString()
        if (keyword.isNotEmpty()) {
            MapUtil.poiSearch(
                this@SetActivity,
                keyword,
                1,
                object : PoiSearch.OnPoiSearchListener {
                    override fun onPoiSearched(p0: PoiResult?, p1: Int) {
                        if (keyword == binding.input.text.toString()) {
                            p0?.pois?.let { adapter.setList(it) }
                        }
                    }

                    override fun onPoiItemSearched(p0: PoiItem?, p1: Int) {
                    }

                })
        }
    }

    override fun afterTextChanged(s: Editable?) {
    }

    @SuppressLint("MissingPermission")
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_OK) {
            MapActivity.getIntentResult(data)?.let { it ->
                // 设置地理围栏
                AwarenessUtil.setLocationBarrier(
                    this, it,
                    {
                        ToastUtils.showShort("设置成功")
                        setResult(RESULT_OK)
                        finish()
                    },
                    { ToastUtils.showShort(it.message) }
                )
            }
        }
    }
}